import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:intl/intl.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    HomeworkPostState state, Dispatch dispatch, ViewService viewService) {
  var border = OutlineInputBorder(
    borderSide: BorderSide(
      color: Colors.grey,
    ),
  );

  List<Widget> classWidgetList = List();
  List<dynamic> classList = state.userInfo['classList'];
  if (classList?.isNotEmpty == true) {
    classWidgetList = classList
        .map<Widget>((klass) => Card(
            margin: EdgeInsets.all(10),
            color: state.selectClasses.contains(klass['code'])
                ? Colors.blue
                : Colors.white,
            child: ListTile(
              onTap: () {
                if (klass["studentTotal"] == 0) {
                  Util.showMsgDialog(
                      '没有学生',
                      '请使用班级邀请码：${klass["code"]} 邀请同学进班级哦！',
                      viewService.context);
                } else {
                  dispatch(
                      HomeworkPostActionCreator.selectClass(klass['code']));
                }
              },
              selected: state.selectClasses.contains(klass['code']),
              title: Row(
                children: <Widget>[
                  Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(klass['name']),
                        Text(klass['gradeText']),
                      ]),
                  Spacer(),
                  Text('${klass['studentTotal']}人'),
                ],
              ),
            )))
        .toList();
  }

  List<DropdownMenuItem> typeItemList() {
    var ret = List<DropdownMenuItem>();
    GlobalState.articleTypes.forEach((type, name) {
      ret.add(DropdownMenuItem(value: type, child: Text(name)));
    });
    return ret;
  }

  Widget progressHeader(int progress) {
    return Padding(
        padding: EdgeInsets.only(top: 6),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Column(
              children: <Widget>[
                Container(
                  width: 36,
                  height: 36,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: Colors.blue,
                  ),
                  padding: EdgeInsets.symmetric(horizontal: 14, vertical: 9),
                  child: Text(
                    '1',
                    style: TextStyle(color: Colors.white, fontSize: 15),
                  ),
                ),
                Text('选择班级', style: TextStyle(fontSize: 9, color: Colors.blue)),
              ],
            ),
            Padding(
              padding: EdgeInsets.only(top: 17),
              child: Container(
                  width: 60,
                  height: 2,
                  color: progress > 0 ? Colors.blue : Colors.black12),
            ),
            Column(
              children: <Widget>[
                Container(
                  width: 36,
                  height: 36,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: progress > 0 ? Colors.blue : Colors.black12,
                  ),
                  padding: EdgeInsets.symmetric(horizontal: 14, vertical: 9),
                  child: Text(
                    '2',
                    style: TextStyle(
                        color: progress > 0 ? Colors.white : Colors.grey,
                        fontSize: 15),
                  ),
                ),
                Text('设置题目',
                    style: TextStyle(
                        fontSize: 9,
                        color: progress > 0 ? Colors.blue : Colors.grey)),
              ],
            ),
            Padding(
              padding: EdgeInsets.only(top: 17),
              child: Container(
                  width: 60,
                  height: 2,
                  color: progress > 1 ? Colors.blue : Colors.black12),
            ),
            Column(
              children: <Widget>[
                Container(
                  width: 36,
                  height: 36,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: progress > 1 ? Colors.blue : Colors.black12,
                  ),
                  padding: EdgeInsets.symmetric(horizontal: 14, vertical: 9),
                  child: Text(
                    '3',
                    style: TextStyle(
                        color: progress > 1 ? Colors.white : Colors.grey,
                        fontSize: 15),
                  ),
                ),
                Text('布置作业',
                    style: TextStyle(
                        fontSize: 9,
                        color: progress > 1 ? Colors.blue : Colors.grey)),
              ],
            ),
          ],
        ));
  }

  var tabClass = Scaffold(
    appBar: AppBar(
      automaticallyImplyLeading: false,
      title: progressHeader(0),
      centerTitle: true,
      elevation: 0,
    ),
    body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 10),
        child: ListView(children: classWidgetList)),
    bottomSheet: Container(
      padding: EdgeInsets.all(10),
      width: double.infinity,
      color: Colors.black12,
      child: Row(
        children: <Widget>[
          Spacer(),
          FlatButton(
            padding: EdgeInsets.symmetric(horizontal: 40, vertical: 10),
            color: Colors.blue,
            textColor: Colors.white,
            child: Text('下一步'),
            shape: RoundedRectangleBorder(
                side: BorderSide(
                  color: Colors.white,
                  width: 0,
                ),
                borderRadius: BorderRadius.circular(20)),
            onPressed: () {
              if (state.selectClasses.length == 0) {
                Util.alert('请选择班级', viewService.context);
              } else {
                dispatch(HomeworkPostActionCreator.next());
              }
            },
          ),
          Spacer(),
        ],
      ),
    ),
  );

  var tabSubject = Scaffold(
    appBar: AppBar(
      automaticallyImplyLeading: false,
      title: progressHeader(1),
      centerTitle: true,
      elevation: 0,
    ),
    body: SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Spacer(),
              FlatButton(
                child: Text(
                  '题库选择',
                  style: TextStyle(color: Colors.blue),
                ),
                onPressed: () =>
                    dispatch(HomeworkPostActionCreator.onSubject()),
              )
            ],
          ),
          Card(
            margin: EdgeInsets.fromLTRB(10, 2, 10, 80),
            color: Colors.white,
            elevation: 2,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(5.0),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  child: DropdownButton(
                    isExpanded: true,
                    underline: Text(''),
                    hint: Text('作文体裁'),
                    value: state.type,
                    items: typeItemList(),
                    onChanged: (item) {
                      dispatch(HomeworkPostActionCreator.selectType(item));
                    },
                  ),
                ),
                Padding(
                    padding: EdgeInsets.symmetric(vertical: 5),
                    child: Divider(height: 1)),
                Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Row(
                      children: <Widget>[
                        Text(
                          '标题',
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.w500),
                        ),
                        Spacer(),
                        SizedBox(
                          width: 60,
                          child: FlatButton(
                            child: Text('编辑',
                                style: TextStyle(color: Colors.blue)),
                            onPressed: () => dispatch(
                                HomeworkPostActionCreator.onEdit('title')),
                          ),
                        ),
                      ],
                    )),
                Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10),
                    child: Offstage(
                      offstage: state.title.isEmpty,
                      child: Text(state.title),
                    )),
                Padding(
                    padding: EdgeInsets.symmetric(vertical: 5),
                    child: Divider(height: 1)),
                Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Row(
                      children: <Widget>[
                        Text('作文要求',
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Spacer(),
                        SizedBox(
                            width: 60,
                            child: FlatButton(
                              child: Text('编辑',
                                  style: TextStyle(color: Colors.blue)),
                              onPressed: () => dispatch(
                                  HomeworkPostActionCreator.onEdit('demand')),
                            )),
                      ],
                    )),
                Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Offstage(
                      offstage: state.demand.isEmpty,
                      child: Text(Util.plainText(state.demand)),
                    )),
                Padding(
                    padding: EdgeInsets.symmetric(vertical: 5),
                    child: Divider(height: 1)),
                Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Row(
                      children: <Widget>[
                        Text('中心词(空格分隔)',
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Spacer(),
                        FlatButton(
                          child: Text('编辑(可选)',
                              style: TextStyle(color: Colors.blue)),
                          onPressed: () => dispatch(
                              HomeworkPostActionCreator.onEdit('keywords')),
                        ),
                      ],
                    )),
                Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Offstage(
                      offstage: state.keywords.isEmpty,
                      child: Text(state.keywords),
                    )),
                Padding(
                    padding: EdgeInsets.symmetric(vertical: 5),
                    child: Divider(height: 1)),
                Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Row(
                      children: <Widget>[
                        Text('构思点拨',
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Spacer(),
                        FlatButton(
                          child: Text('编辑(可选)',
                              style: TextStyle(color: Colors.blue)),
                          onPressed: () => dispatch(
                              HomeworkPostActionCreator.onEdit('ideation')),
                        ),
                      ],
                    )),
                Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Offstage(
                      offstage: state.ideation.isEmpty,
                      child: Text(Util.plainText(state.ideation)),
                    )),
                SizedBox(height: 10),
              ],
            ),
          )
        ],
      ),
    ),
    bottomSheet: Container(
      padding: EdgeInsets.all(10),
      width: double.infinity,
      color: Colors.black12,
      child: Row(
        children: <Widget>[
          Spacer(),
          FlatButton(
            padding: EdgeInsets.symmetric(horizontal: 40, vertical: 10),
            color: Colors.blue,
            textColor: Colors.white,
            child: Text('下一步'),
            shape: RoundedRectangleBorder(
                side: BorderSide(
                  color: Colors.white,
                  width: 0,
                ),
                borderRadius: BorderRadius.circular(20)),
            onPressed: () {
              if (state.title.isEmpty && state.demand.isEmpty) {
                Util.alert('请填写题目或标题', viewService.context);
              } else {
                dispatch(HomeworkPostActionCreator.next());
              }
            },
          ),
          Spacer(),
        ],
      ),
    ),
  );

  var tabPost = Scaffold(
    appBar: AppBar(
      automaticallyImplyLeading: false,
      title: progressHeader(2),
      centerTitle: true,
      elevation: 0,
    ),
    body: ListView(
      children: <Widget>[
        ListTile(
          onTap: () => dispatch(HomeworkPostActionCreator.onStartTime()),
          title: Text('开始时间',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
          trailing: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text(DateFormat('yyyy/MM/dd kk:mm:ss').format(state.start)),
              ImageIcon(
                AssetImage("assets/images/right.png"),
                size: 24.0,
              ),
            ],
          ),
        ),
        ListTile(
          onTap: () => dispatch(HomeworkPostActionCreator.onEndTime()),
          title: Text('截止时间',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
          trailing: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text(DateFormat('yyyy/MM/dd kk:mm:ss').format(state.end)),
              ImageIcon(
                AssetImage("assets/images/right.png"),
                size: 24.0,
              ),
            ],
          ),
        ),
        ExpansionTile(
          title: const Text('高级选项'),
          children: <Widget>[
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('最小字数',
                        style: TextStyle(fontSize: 14, color: Colors.black)),
                    Row(
                      children: <Widget>[
                        Text((state.minCount.toInt() * 50).toString(),
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Expanded(
                            child: Slider.adaptive(
                          max: 40,
                          min: 0,
                          value: state.minCount,
                          onChanged: (count) => dispatch(
                              HomeworkPostActionCreator.value(
                                  {'minCount': count})),
                        )),
                      ],
                    ),
                    Divider(height: 1),
                    Text('最大字数',
                        style: TextStyle(fontSize: 14, color: Colors.black)),
                    Row(
                      children: <Widget>[
                        Text((state.maxCount.toInt() * 50).toString(),
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Expanded(
                            child: Slider(
                          label: '字数限制2',
                          max: 40,
                          min: 0,
                          value: state.maxCount,
                          onChanged: (count) => dispatch(
                              HomeworkPostActionCreator.value(
                                  {'maxCount': count})),
                        )),
                      ],
                    ),
                    Divider(height: 1),
                    Text('期望机评最低分',
                        style: TextStyle(fontSize: 14, color: Colors.black)),
                    Row(
                      children: <Widget>[
                        Text(state.minScore.toInt().toString(),
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Expanded(
                            child: Slider(
                          label: '字数限制2',
                          max: 100,
                          min: 0,
                          value: state.minScore,
                          onChanged: (count) => dispatch(
                              HomeworkPostActionCreator.value(
                                  {'minScore': count})),
                        )),
                      ],
                    ),
                    Divider(height: 1),
                    Text('期望机评最高分',
                        style: TextStyle(fontSize: 14, color: Colors.black)),
                    Row(
                      children: <Widget>[
                        Text(state.maxScore.toInt().toString(),
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Expanded(
                            child: Slider(
                          label: '字数限制2',
                          max: 100,
                          min: 0,
                          value: state.maxScore,
                          onChanged: (count) => dispatch(
                              HomeworkPostActionCreator.value(
                                  {'maxScore': count})),
                        )),
                      ],
                    ),
                    Divider(height: 1),
                    Text('低于该分数不能交作业',
                        style: TextStyle(fontSize: 14, color: Colors.black)),
                    Row(
                      children: <Widget>[
                        Text((state.passScore.toInt() * 5).toString(),
                            style: TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w500)),
                        Expanded(
                            child: Slider(
                          label: '字数限制2',
                          max: 20,
                          min: 0,
                          value: state.passScore,
                          onChanged: (count) => dispatch(
                              HomeworkPostActionCreator.value(
                                  {'passScore': count})),
                        )),
                      ],
                    ),
                    SizedBox(height: 100),
                  ],
                )),
          ],
        ),
      ],
    ),
    bottomSheet: Container(
      padding: EdgeInsets.all(10),
      width: double.infinity,
      color: Colors.black12,
      child: Row(
        children: <Widget>[
          Spacer(),
          FlatButton(
            padding: EdgeInsets.symmetric(horizontal: 40, vertical: 10),
            color: Colors.blue,
            textColor: Colors.white,
            child: Text('布置作业'),
            shape: RoundedRectangleBorder(
                side: BorderSide(
                  color: Colors.white,
                  width: 0,
                ),
                borderRadius: BorderRadius.circular(20)),
            onPressed: () {
              dispatch(HomeworkPostActionCreator.onDone());
            },
          ),
          Spacer(),
        ],
      ),
    ),
  );

  return WillPopScope(
      onWillPop: () async {
        if (state.controller.page >= 1) {
          state.controller.previousPage(
              duration: Duration(milliseconds: 300), curve: Curves.linear);
          return false;
        }
        return true;
      },
      child: Scaffold(
        appBar: new AppBar(
            centerTitle: true,
            elevation: 2.0,
            title: new Text(
              "布置作业",
              style: TextStyle(color: Colors.black54, fontSize: 18),
            )),
        body: PageView(
          physics: new NeverScrollableScrollPhysics(), // disable swipe
          controller: state.controller,
          onPageChanged: (index) => print(index),
          children: <Widget>[
            tabClass,
            tabSubject,
            tabPost,
          ],
        ),
      ));
}
